package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.resource.date.DateUtils;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderSrviceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    //用它来查询t_ordersetting这张表调用方法来判断已预约的人数是否已满
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    //用它来查询用户的手机号
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Order add(Map map) {


        try {
            //先获取前端页面用户选择的时间
            String orderDate = (String) map.get("orderDate");
            //获取预约的id
            String  setmeal_Id = (String)map.get("setmealId");
            int setmealId = Integer.parseInt(setmeal_Id);
            //获取页面输入的手机号
            String telephone = (String) map.get("telephone");
            //获取用户名字
            String name = (String) map.get("name");
            //获取性别
            String sex = (String) map.get("sex");
            //获取身份证
            String idCard = (String) map.get("idCard");
            //获取还有多少人可以预约，查询（人数）
            int count = orderSettingMapper.isOrderfull(orderDate);
            //1.如果大于0也就是可以预约，然后把要预约的表单中的数据封装到Order实体类中去，再把它传入数据库
            if (count > 0) {
                Order order = new Order();
                //封装时间，先把时间的日期格式转换
                Date date = DateUtils.parseString2Date(orderDate);
                order.setOrderDate(date);
                order.setOrderStatus(Order.ORDERSTATUS_NO);
                order.setOrderType(Order.ORDERTYPE_WEIXIN);
                order.setSetmealId(setmealId);
                //order.setMemberId(member.getId());//这时候不能插入id，因为不知道有没有该用户

                //2.预约表，插入数据之后，以预约人数随之添加，也就要更新t_ordersetting这张表
                orderSettingMapper.updateReseversByData(orderDate);
                //3.根据手机号判断用户是否存在，如果不存在执行insert操作
                Member member = memberMapper.findMemberByTelephone(telephone);
                if (member == null) {
                    //新会员insert
                    member = new Member();
                    member.setPhoneNumber(telephone);
                    member.setRegTime(new Date());
                    member.setName(name);
                    member.setIdCard(idCard);
                    member.setSex(sex);
                    memberMapper.insert(member);
                } else {
                    //老会员getId()获取数据，当然老会员也有可能该套餐当天预约过，再次预约该套餐要显示已预约过
                    //查找预约的日期，预约套餐id和用户id存不存在,查出存在的人数
                    int orderCount = baseMapper.findOrderIsExistByOrderDateAndSetmealIdAndMemberId(orderDate, setmealId, member.getId());
                    if (orderCount > 0) {
                        System.out.println(orderCount);
                        //如果大于0就证明存在，提醒他已经预约过了
                        throw new RuntimeException("已预约过，请勿重复预约");
                        //这地方抛异常就证明已经该用户已经预约过了，但是前面数据已经插入，按照常理来说应该回滚数据
                    }
                }
                //判断完是否为新老用户，这时候才可以设置id
                order.setMemberId(member.getId());

                //表单数据获取完成，添加订单
                baseMapper.insert(order);
                return order;
            } else {
                throw new RuntimeException("预约人数已满,请改天预约");
            }
        } catch (Exception e) {
            //打印异常
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public Map findOrderMessage(Integer orderid) {
        Map ordermap = baseMapper.findOrderMessagebyid(orderid);
        return ordermap;
    }
}
